import { getBaseCategoryList, getFloorList,getRankList, getRemaiList, getTeJiaList, getXinPinList } from "@/api/product";

const state = {
  categoryList : [],
  floorList:[],
  rankList:[],
  rankList2:[],
};
const mutations = {
  SET_CATEGORYLIST(state,categoryList){
    state.categoryList = categoryList
  },
  SET_FLOORLIST(state,floorList){
    state.floorList = floorList;
  },
  SET_RANKLIST(state,rankList){
    // console.log(111)
    state.rankList = rankList
  },
  SAVE_RANK_LIST2(state,rankList){
    // console.log(111)
		state.rankList2 = rankList;
	},
};
const actions = {
  //获取三级分类
  async getCategoryListAsync({commit},num){
      //因为拦截器已经处理失败的情况，返回padding状态，中断执行了。所以要么成功，要么中断（result永远收不到值）。
      //result是请求体
      const result = await getBaseCategoryList();
      if(result.code === 200) {
         //splice：第一个参数索引，第二个参数删除个数，第三个及以上是插入元素。返回删除的元素的数组
        commit('SET_CATEGORYLIST',result.data.splice(0,num))
      } else {
        alert('请求成功，但是数据获取失败')
      }
  },
  //获取楼层
  async getFloorListAsync({commit}){
    const result = await getFloorList();
    commit('SET_FLOORLIST',result.data)
  },
  //获取排行
  async getRankListAsync({commit}){
    const result = await getRankList();
    // console.log(result)
    commit('SET_RANKLIST',result.data)
  },

  //获取排名2
  async getRankList2Async({commit},type){
    //type：1：热卖 2：特价 3：新品
    let result={};
    // console.log(type)
    if (type === 1) {
      result = await getRemaiList();
    } else if (type === 2) {
      result = await getTeJiaList();
    } else {
      result = await getXinPinList();
    }
    commit('SAVE_RANK_LIST2',result.data)
  }
};
const getters = {};

export default {
  state,
  mutations,
  actions,
  getters,
  namespaced:true,
}